package com.cjk.ssm.controller;

import com.cjk.ssm.entity.Department;
import com.cjk.ssm.exp.MyDeleteFailException;
import com.cjk.ssm.exp.MyNoDataException;
import com.cjk.ssm.exp.MyUpdFailException;
import com.cjk.ssm.service.DepartmentService;
import com.cjk.ssm.service.impl.DepartmentServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 部门--控制层
 */
@Controller
@RequestMapping("/dept")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @RequestMapping(value = {"", "/", "/index"})
    public String index(@RequestParam(value = "pageNum", required = false) Integer pageNume,
                        Model model) {
        try {
            PageInfo<Department> pageInfo
                    = departmentService.findAllPage(pageNume, 2);
            System.out.println(pageInfo);
            model.addAttribute("page", pageInfo);
        } catch (MyNoDataException e) {
            // 数据库无数据
            model.addAttribute("msg", "暂无数据");
        } catch (Exception e) {
            // 其他异常
            model.addAttribute("msg", "系统繁忙");
        }
        return "department_index";
    }

    @RequestMapping("/addpage")
    public String addpage(){
        return "department_add_page";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(Department department,Model model){
        System.out.println(department);
        try {

            departmentService.add(department);
        }catch (MyNoDataException e){
            //保存失败，跳转到失败提示页面
            model.addAttribute("msg","亲,保存失败~");
            return "err_page";
        }
        catch (Exception e) {
            //其他异常情况，跳转到异常提示页面s
            model.addAttribute("亲，系统繁忙~");
            return "err_page";
        }
        //重定向
        return "redirect:/dept";
    }
    @RequestMapping("/del")
    public String del(@RequestParam(value="id",required = true) Integer id,Model model){
        try {
            System.out.println(id);
            departmentService.del(id);
            return "redirect:/dept";//删除成功返回首页
        } catch (MyDeleteFailException e) {
            //删除失败
            model.addAttribute("msg","删除失败");
            return "err_page";
        }catch (Exception e) {
            //其他错误
            model.addAttribute("msg", "系统繁忙");
            return "err_page";
        }
    }
    //修改
    @RequestMapping(value = "/toUpdpage/{id}")
    public String toUpdpage(@PathVariable("id") Integer id,Model model){
        Department department;
        try {
           department=departmentService.findById(id);
        } catch (MyUpdFailException e) {
           model.addAttribute("msg","修改失败");
           return "err_page";
        }catch (Exception e){
            model.addAttribute("msg","系统繁忙");
            return "err_page";
        }
        model.addAttribute("department",department);
        return "department_upd_page";
    }
    @RequestMapping(value = "/upd",method = RequestMethod.POST)
    public String upd(Department department,Model model) {
        try {
            departmentService.upd(department);
        } catch (MyUpdFailException e) {
            //修改失败，跳转到失败提示页面
            model.addAttribute("msg", "亲，修改失败~");
            return "err_page";
        } catch (Exception e) {
            model.addAttribute("msg", "系统繁忙~");
            return "err_page";
        }
        //重定向
        return "redirect:/dept";
    }
//   模糊查找
    @RequestMapping(value = "/findbyname",method = RequestMethod.GET)
    public String findByDeptNameLikePage(@RequestParam(value = "name") String name,
                                         @RequestParam(value = "pageNum",required = false) Integer pageNume,
                                         Model model){
        try {
            PageInfo<Department> pageInfo = departmentService.findByDeptNameLikePage(name, pageNume, 2);
            System.out.println(pageInfo);
            model.addAttribute("name",name);
            model.addAttribute("page",pageInfo);
        } catch (MyNoDataException e) {
            model.addAttribute("msg","暂无数据");
        }catch (Exception e){
            model.addAttribute("msg","系统繁忙");
        }
        return "department_dept_index";
    }
}
